package com.wry.wry_test.utils;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.parser.OpenAPIV3Parser;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Map;

public class SwaggerParseUtil {


    public static void main(String[] args) throws Exception {
        String path1 = "E:\\工作文档\\data\\iserver-openapi-public-all.yaml";
        String path2 = "E:\\工作文档\\data\\sample.yaml";
        String path3 = "E:\\工作文档\\data\\sample.json";
        String path4 = "E:\\工作文档\\data\\接口文档.openapi.yaml";
        String path5 = "E:\\工作文档\\data\\iserver.openapi.yaml";
        String path6 = "E:\\工作文档\\data\\iserver.openapi.json";
        String path7 = "E:\\工作文档\\data\\iserver-openapi-public-all.json";
        String path8 = "E:\\工作文档\\data\\iserver-openapi-public-simple.yaml";

        String json = JsonVerify.yamlToJson(path7);
        System.out.println(json);
        // 构建OpenAPIV3Parser实例
        OpenAPIV3Parser parser = new OpenAPIV3Parser();


        ObjectMapper objectMapper = new ObjectMapper();
        ObjectMapper yamlMapper = new ObjectMapper(new YAMLFactory());

        // 将JSON字符串转换为Java对象
        Object obj = objectMapper.readValue(json, Object.class);
        // 将Java对象转换为YAML字符串
        String yamlOutput = yamlMapper.writeValueAsString(obj);
        System.out.println(yamlOutput);

        // 指定YAML文件路径
        File file = new File(path8);
        // 解析YAML文件
        json = JSONUtil.jsonWashing(json,"example");
        OpenAPI openAPI = parser.readContents(yamlOutput).getOpenAPI();
        OpenAPI openApi = objectMapper.readValue(json.getBytes(StandardCharsets.UTF_8), OpenAPI.class);
        // 打印解析后的OpenAPI对象信息，根据需要处理这个对象
        System.out.println(openApi);

    }

    public static void getSwaggerUrl(String url) {


    }


}
